package com.situ.mall.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.situ.mall.base.entity.Member;
import com.situ.mall.base.exception.InfoException;
import com.situ.mall.base.mapper.MemberMapper;
import com.situ.mall.base.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.base.util.*;
import com.situ.mall.base.vo.LayuiTableVO;
import com.situ.mall.base.vo.ResVO;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author darling
 * @since 2023-03-20
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Override
    public LayuiTableVO doList(Integer page, Integer limit, String keyword, Integer status) {
        PageHelper.startPage(page, limit);
        return LayuiTableVO.data(baseMapper.doList(keyword, status));
    }

    @Override
    public void start(Integer id) {
        baseMapper.start(id);
    }

    @Override
    public void stop(Integer id) {
        baseMapper.stop(id);
    }

    @Override
    public Member selectByMemberName(String username) {
        return baseMapper.selectByUsername(username);
    }

    @Override
    public boolean check(String username, String password) {
        Member m = baseMapper.selectByUsername(username);
        if (m == null) {
            throw new InfoException("用户名不存在！", 500);
        }
        if (m.getPassword().equals(password)) {
            OrderSessionUtil.setCurrent(m);
            return true;
        }
        return false;
    }

    @Override
    public synchronized ResVO avatar(MultipartFile file) {
        AvatarUtil avatarUtil = new AvatarUtil();
        String path = C.AVATAR_PATH;
        String filename = SnowFlake.nextId() + C.PNG;
        return avatarUtil.avatar(file, path, filename);
    }


}
